package com.danny.demo.entity;

import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import java.io.*;

/**
 * @ClassName: UserSerializable
 * @Description: TODO
 * @Author: zhangdong
 * @Date: Created in 11:12 2019/6/19
 * @Version:
 */
public class UserSerializable {

    public static void main(String[] args) {

        User user = new User();
        user.setUserName("Danny");
        user.setUserAge("20");
        user.setUserSex("男");

        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("tempFile"));
            oos.writeObject(user);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            IOUtils.closeQuietly(oos);
        }

        //Read Obj From File
        File file = new File("tempFile");
        ObjectInputStream ois = null;
        try {
            ois =new  ObjectInputStream(new FileInputStream(file));
            User newUser = (User)ois.readObject();
            System.out.println(newUser);
        }catch (IOException e){
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally{
            IOUtils.closeQuietly(ois);
            try {
                FileUtils.forceDelete(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
